// pages/details/details.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    id2:'',
    content:'',
    show:false,
    user:'{"tx":"https://thirdwx.qlogo.cn/mmopen/vi_32/vYOPewuv6LjvPibudDrrB9dRDUwGqV7qfoibIXGrwyHNwVgribzibJaFf18YBkGicqNQaZa8WceoWMNzZJeCcq86EXg/132","name":"『Silence』&#128347;"}',
    detailsData:{},
    recommentData:{},
    talkList:[],
    recommentList:[],
    type:1,
  },
  toLikes(e){
    let {talkList,recommentList} = this.data;
    let index=e.currentTarget.dataset.index
    if(talkList.length>0){
      talkList[index].like=!talkList[index].like
      this.setData({
        talkList
      })
    }else{
      recommentList[index].like=!recommentList[index].like
      this.setData({
        recommentList
      })
    }
  },
  // 获取数据
  getData(){
    let that=this;
    let {id}=this.data;
    wx.request({
      url: 'http://localhost:8888/homeData',
      success:(res)=>{
        that.setData({
          content:''
        })
        res.data.data.filter((item)=>{
          if(id===item.id+''){
            if(item.collection){
              item.collection=JSON.parse(item.collection)
            }else{
              item.collection=[]
            }
            if(item.comment){
              item.comment=JSON.parse(item.comment).map((item)=>{
                item.name1 = that.StringExchangeEmoji(item.name)
                item.like=false
                return item
              })
            }else{
              item.comment=[]
            }
            let is=false
            is = item.collection.filter((item)=>item.name==JSON.parse(that.data.user).name).length>0?true:false;
            item.iscollection = item.collection?is?true:false:false
            console.log(item)
            that.setData({
              detailsData:item,
              talkList:item.comment
            })
          }
        })
      }
    })
  },
  // 请求热门推荐数据
  getRecommentList(){
    let that=this;
    let {id2}=this.data;
    wx.request({
      url: 'http://localhost:8888/getZp',
      success:(res)=>{
        that.setData({
          content:''
        })
        res.data.data.filter((item)=>{
          item.userid=JSON.parse(item.userid)
          item.img=JSON.parse(item.img)
          if(id2===item.id+''){
            if(item.collection){
              item.collection=JSON.parse(item.collection)
            }else{
              item.collection=[]
            }
            if(item.comment){
              item.comment=JSON.parse(item.comment).map((item)=>{
                item.name1 = that.StringExchangeEmoji(item.name)
                item.like=false
                return item
              })
            }else{
              item.comment=[]
            }
            let is=false
            is = item.collection.filter((item)=>item.name==JSON.parse(that.data.user).name).length>0?true:false;
            item.iscollection = item.collection?is?true:false:false
            console.log(item)
            that.setData({
              recommentData:item,
              recommentList:item.comment
            })
          }
        })
      }
    })
  },
  // 收藏
  toCollection(e){
    let {id,collection,iscollection}=e.currentTarget.dataset.item
    let _this=this
    if(iscollection){
      let ind = ''
      collection.filter((item,index)=>{
        if(item.name == JSON.parse(_this.data.user).name){
          ind = index
        }
      })
      collection.splice(ind,1)
    }else{
      collection.push(JSON.parse(_this.data.user))
    }
    collection = JSON.stringify(collection)
    wx.request({
      url: 'http://localhost:8888/editcollection',
      method:'POST',
      data:{id,collection},
      success:(res)=>{
        if(res.data.code==200){
          _this.getData()
        }
      }
    })
  },
  // 收藏2
  toCollection2(e){
    console.log(1)
    let {id,collection,iscollection}=e.currentTarget.dataset.item
    let _this=this
    if(iscollection){
      let ind = ''
      collection.filter((item,index)=>{
        if(item.name == JSON.parse(_this.data.user).name){
          ind = index
        }
      })
      collection.splice(ind,1)
    }else{
      collection.push(JSON.parse(_this.data.user))
    }
    collection = JSON.stringify(collection)
    wx.request({
      url: 'http://localhost:8888/recommendCollection',
      method:'POST',
      data:{id,collection},
      success:(res)=>{
        if(res.data.code==200){
          _this.getRecommentList()
        }
      }
    })
  },
  bindKeyInput(e){
    this.setData({
      content:e.detail.value
    })
  },
  // 评论
  toInput(e){
    let _this=this
    let {id,comment} = e.currentTarget.dataset.item
    var util=require('../../utils/util')
    var data=util.formatTime(new Date())
    let obj=JSON.parse(_this.data.user)
    obj.content=_this.data.content
    obj.time=data
    console.log(obj)
    comment.push(obj)
    comment= comment.map((item)=>{
      item.name1=''
      return item
    })
    comment = JSON.stringify(comment)
    wx.request({
      url: 'http://localhost:8888/wlkcomment',
      method:'POST',
      data:{id,comment},
      success:(res)=>{
        if(res.data.code==200){
          _this.getData()
        }
      }
    })
  },
  // 评论2
  toInput2(e){
    console.log(111)
    let _this=this
    let {id,comment} = e.currentTarget.dataset.item
    var util=require('../../utils/util')
    var data=util.formatTime(new Date())
    let obj=JSON.parse(_this.data.user)
    obj.content=_this.data.content
    obj.time=data
    console.log(obj)
    comment.push(obj)
    comment= comment.map((item)=>{
      item.name1=''
      return item
    })
    comment = JSON.stringify(comment)
    wx.request({
      url: 'http://localhost:8888/recommendComment',
      method:'POST',
      data:{id,comment},
      success:(res)=>{
        if(res.data.code==200){
          _this.getRecommentList()
        }
      }
    })
  },
  toShow(){
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
   // 变成emoji
  StringExchangeEmoji(str){
      var reg = /\&#.*?;/g;
      var result = str.replace(reg,function(char){
          var H,L,code;
          if(char.length == 9 ){
              code = parseInt(char.match(/[0-9]+/g));
              H = Math.floor((code-0x10000) / 0x400)+0xD800;
              L = (code - 0x10000) % 0x400 + 0xDC00;
              return unescape("%u"+H.toString(16)+"%u"+L.toString(16));
          }else{
              return char;
          }
      });
      return result;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      user:wx.getStorageSync('user')
    })
    console.log(options)
    let that=this;
    if(options.type&&options.id2){
      that.setData({
        type:2,
        id2:options.id2
      })
      that.getRecommentList()
    }else{
      that.setData({
        id:options.id
      })
      that.getData()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})